﻿using STT.Business;
using STT.Entities;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Admin.UserControls
{
    public partial class ucTinTuc : System.Web.UI.UserControl
    {
        public bool chonSuaClick;
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadTinTuc();
        }

        void LoadTinTuc()
        {
            gvTinTuc.AutoGenerateColumns = false;
            gvTinTuc.DataSource = BTbTinTuc.SelectAll();
            gvTinTuc.DataBind();
        }
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>confirm('" + Loi + "')</script>", false);
        }
        protected void gvTinTuc_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvTinTuc.EditIndex = e.NewEditIndex;
            gvTinTuc.DataBind();
        }

        protected void gvTinTuc_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvTinTuc.EditIndex = -1;
            gvTinTuc.DataBind();
        }   

        protected void lbXuatDuLieu_Click(object sender, EventArgs e)
        {

        }

        public string Check(string MaLoaiTT)
        {
            return "ds";
        }

        protected void lbXoaTat_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < gvTinTuc.Rows.Count; i++)
                {
                    CheckBox cbChon = (CheckBox)gvTinTuc.Rows[i].Cells[0].FindControl("cbChon");
                    if (cbChon != null && cbChon.Checked)
                    {
                        BTbTinTuc.Delete(int.Parse(gvTinTuc.DataKeys[i].Value.ToString()));
                    }
                }

                LoadTinTuc();
                Session["action"] = 3;
            }
            catch
            {
                ThongBao("Dữ liệu liên quan đến Khóa học này vẫn tồn tại, vui lòng kiểm tra lại dữ liệu!");
            }
        }

        // Phân trang
        protected void gvTinTuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTinTuc.PageIndex = e.NewPageIndex;
            gvTinTuc.DataBind();
        }
        public static String maTT;
        protected void gvTinTuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ChonSua")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                maTT = gvTinTuc.DataKeys[index].Values[0].ToString();

                Response.Redirect("~/Admin/Default.aspx?idpage=itt&idtt=" + maTT);
            }  
        }
 
        protected void btSearch_Click(object sender, ImageClickEventArgs e)
        {
            gvTinTuc.DataSource = BTbTinTuc.SearchTinTuc(new ETbTinTuc
            {
                TieuDe = txtTimKiem.Text
            });
            gvTinTuc.DataBind();
        }

        protected void btImport_Click(object sender, EventArgs e)
        {

        }
        protected void btThemExcel_Click(object sender, EventArgs e)
        {

        }

        public string CatTu(string str)
        {
            int len= str.LastIndexOf(" ");
            int length = len > 2000 ? len / 8 : len;

            if (length == -1) length = str.Length;

            return str = HttpUtility.HtmlDecode(str.Substring(0, length) + "<b>...<b>");
        }
        protected void gvTinTuc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int maTT = int.Parse(gvTinTuc.DataKeys[e.RowIndex].Value.ToString());

            BTbTinTuc.Delete(maTT);
            LoadTinTuc();
            Session["action"] = 3;
        }
    }
}